//
//  ContentTableViewController.m
//  CarHome
//
//  Created by lanou3g on 15-4-30.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "SummaryTableViewController.h"
#import "SeriesInfo.h"
#import "Engine.h"
#import "EngineSpecification.h"
#import "UIImageView+WebCache.h"
#import "SummaryHeaderViewController.h"
#import "SummaryTableViewCell.h"


@interface SummaryTableViewController ()

@property (nonatomic, retain) NSMutableArray *enginelist;
@property (nonatomic, retain) SeriesInfo *si;

@end

@implementation SummaryTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"SummaryTableViewCell" bundle:nil] forCellReuseIdentifier:@"summaryTVcell"];
    
    self.enginelist = [NSMutableArray array];
    [self loadData];
}

- (void)loadData {
    if (_enginelist.count == 0) {
        [self.tableView addMyActivityIndicatorView2];
        __block typeof(self) weakSelf = self;
        
        NSLog(@"%@", summaryURLwithCarID(_seriesID));
        
        [HTTPRequest requestWithURL:summaryURLwithCarID(_seriesID) method:@"GET" result:^(NSData *data) {
            NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            self.si = [[SeriesInfo new] autorelease];
            [_si setValuesForKeysWithDictionary:dict[@"result"]];
            [self loadTVHeaderView];
            self.enginelist = [NSMutableArray arrayWithArray:_si.enginelist];
            [self.tableView removeMyActivityIndicatorViewFromSuperView];
            [weakSelf.tableView reloadData];
            
        }];
    } else {
        [self.tableView reloadData];
    }
    

}

- (void)loadTVHeaderView {
    /*
    UIImageView *headerView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWIDTH, kHEIGHT / 3)] autorelease];
    [headerView sd_setImageWithURL:[NSURL URLWithString:_si.logo] placeholderImage:[UIImage imageNamed:@"black_default_640*320"]];
    
    UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, kHEIGHT / 3 - 18, 200, 15)];
    leftLabel.textColor = [UIColor whiteColor];
    [leftLabel setFont:[UIFont systemFontOfSize:12]];
    leftLabel.text = [NSString stringWithFormat:@"%@/%@", [_si levelname], [_si paramlist][1][@"value"]];
    
    UILabel *rightLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWIDTH - 80, kHEIGHT / 3 - 18, 100, 15)];
    rightLabel.textColor = [UIColor whiteColor];
    [rightLabel setFont:[UIFont systemFontOfSize:12]];
    rightLabel.text = [NSString stringWithFormat:@"共%@张图", [_si piccount]];
    
    UIView *bottomImage = [[UIView alloc] initWithFrame:CGRectMake(0, kHEIGHT / 3 - 18, kWIDTH, 18)];
    bottomImage.backgroundColor = [UIColor blackColor];
    bottomImage.alpha = .01;
    [headerView addSubview:leftLabel];
    [headerView addSubview:rightLabel];
    [headerView addSubview:bottomImage];
     */
    SummaryHeaderViewController *summaryHeaderVC = [[SummaryHeaderViewController new] autorelease];
    summaryHeaderVC.si = _si;
    
    UITapGestureRecognizer *tpr3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tpr3Action:)];
    tpr3.numberOfTapsRequired = 1;
    
    [summaryHeaderVC.view.subviews[0] addGestureRecognizer:tpr3];
    
    self.tableView.tableHeaderView = summaryHeaderVC.view;
}

- (void)tpr3Action:(UITapGestureRecognizer *)sender {
    UIScrollView *scroView = (UIScrollView *)self.view.superview;
    
    [scroView setContentOffset:CGPointMake(kWIDTH, 0) animated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _enginelist.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    Engine *engine = _enginelist[section];
    return engine.speclist.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    SummaryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"summaryTVcell"];
    
    if (cell == nil) {
        
        cell = [[SummaryTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"summaryTVcell"];

    }
    
    Engine *e = _enginelist[indexPath.section];
    NSMutableArray *array = [NSMutableArray arrayWithArray:e.speclist];
    EngineSpecification *es = array[indexPath.row];
    cell.name.text = es.name;
    cell.price.text = es.price;
    cell.minprice.text = [es.minprice isEqualToString:@""] ? @"无" : es.minprice;
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [_enginelist[section] name];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 150;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
